<?php

class ArmoryAPITest
{

        const useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.0; de-DE; rv:1.6) Gecko/20040206 Firefox/1.0.1';
        const char_profile_url = 'http://eu.wowarmory.com/character-sheet.xml?r=%s&n=%s&gn=%s';
        const char_achievements_url = 'http://eu.wowarmory.com/character-achievements.xml?r=%s&n=%s&gn=%s';
        const char_talents_url = 'http://eu.wowarmory.com/character-talents.xml?r=%s&n=%s&gn=%s';
        const char_reputation_url = 'http://eu.wowarmory.com/character-reputation.xml?r=%s&n=%s&gn=%s';
        const guild_url = 'http://eu.wowarmory.com/guild-info.xml?r=%s&cn=%s&gn=%s';

        public $sCharacterName, $sRealmName, $sGuildName, $sArmoryUrl, $sXmlData;

        /**
         * Constructor
         * 
         * @param string $realm Name of Realm
         * @param string $guild Name of guild
         * @param string $character Name of character
         * @param string $type Choose: 'achievements', 'talents', 'reputation', 'guildinfo', 'profile' [optional]
         */
        public function __construct($realm, $guild, $character, $type = 'profile')
        {
                $this->setRealmName($realm);
                $this->setCharacterName($character);
                $this->setGuildName($guild);
                $this->bProxy = $use_proxy;

                // set sheet by type of data
                switch($type):
                        case 'achievements':
                                $sheet = self::char_achievements_url;
                        break;
                        case 'talents':
                                $sheet = self::char_talents_url;
                        break;
                        case 'reputation':
                                $sheet = self::char_reputation_url;
                        break;
                        case 'guildinfo':
                                $sheet = self::guild_url;
                        break;
                        default:
                                $sheet = self::char_profile_url;
                        break;
                endswitch;

                $this->sArmoryUrl = sprintf($sheet, $this->sRealmName, $this->sCharacterName, $this->sGuildName);
                echo $this->sArmoryUrl;

                // run
                $this->fetchArmoryData();

        }

        /**
         * Set name of character
         * @param string $name Name of character
         */
        public function setCharacterName($name)
        {
                $this->sCharacterName = urlencode($name);
        }

        /**
         * Set name of realm
         * @param string $name Name of realm
         */
        public function setRealmName($name)
        {
                $this->sRealmName = urlencode($name);
        }

        /**
         * Set guild name
         * @param string $name Name of guild
         */
        public function setGuildName($name)
        {
                $this->sGuildName = urlencode($name);
        }

        /**
         * Fetch XML data
         */
        public function fetchArmoryData()
        {
                $c = curl_init();
                curl_setopt($c, CURLOPT_URL, $this->sArmoryUrl);
                curl_setopt($c, CURLOPT_USERAGENT, self::useragent);
                curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
                $this->sXmlData = curl_exec($c);
                curl_close($c);
        }

        /**
         * Parse XML (and return)
         * @return object SimpleXML object of armory data
         */
        public function parseData()
        {
                return new SimpleXMLElement($this->sXmlData);
        }

}

?>